/**
 * Created by caosh on 2018/1/17 0017.
 */
var Index = new Vue({
    el: '#index',
    data: {
        num: 1,
        numObj: {}, // 用于记录所有购物车里的数据
        product: {}, // 用于复制
        showCity: false,
        isShowMask: false,
        searchValue: '',
        selected: 'home',
        panelHeight: 0,
        recommend: {},
        consumerHotline: '', // 客服电话
        oftenBuyList: [], // 常购清单
        recommends: [] // 推荐
    },
    mixins:[CartMixin, ProductMixin, SystemMixin],
    watch: {
        selected: function (val, oldVal) {
            if (val != oldVal) { // tabbar切换
                if (val == 'all') {
                    window.location.href = './product/index.html';
                } else if (val == 'cart') {
                    window.location.href = './cart/index.html';
                } else if (val == 'mine') {
                    window.location.href = './mine/index.html';
                }
            }
        },
        num: function (val, oldVal) {
            if (val <= 0 && val != '') {
                this.num = 1;
            }
        }
    },
    methods: {
        /*selectInputFocus: function () {
            $('#cart-product-num').select();
        },*/
        // 跳转查询页面
        toSearch: function () {
            window.location.href='./search/index.html';
        },
        // 显示/隐藏规格
        toShowNorm: function (p) {
            p.showNorm = !p.showNorm;
        },
        removeOftenBuy: function (p) {
            // 移除常购清单里选中的数据
            this.oftenBuyList.splice(this.oftenBuyList.indexOf(p), 1);
            this.$toast({
                message: '删除成功',
                iconClass: 'iconfont icon-success iconfont-g',
                className: 'bsz-toast',
                duration: 1000
            });
        },
        initProduct: function (p, n) {
            this.product = this.getProductFromPool(p, n);
            this.product.unitPrice = n.price;
            this.product.unit = n.unit;
            this.product.nId = n.id;
            this.num = this.product.num;
        },
        confirmProduct: function (NO) {
            this.product.num = NO;
            if (NO > 0 && this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }

            this.triggerRemoveProduct(NO, this.product);
        },
        clickReduce: function (p, n) {
            this.numObj[p.id+'-'+n.id]--;
            this.confirmProduct(this.numObj[p.id+'-'+n.id]);
        },
        clickPlus: function (p, n) {
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }

            if (!n.hasOwnProperty('showControl')) {
                Vue.set(n, 'showControl', true);
            }

            /*var NO = this.numObj[p.id+'-'+n.id];
            if (!NO) {
                this.numObj[p.id+'-'+n.id] = 1;
            } else {
                this.numObj[p.id+'-'+n.id]++;
            }*/

            this.initProduct(p, n);
            var NO = this.numObj[p.id+'-'+n.id];
            if (!NO && NO != 0) {
                this.numObj[p.id+'-'+n.id] = this.num;
            } else {
                this.numObj[p.id+'-'+n.id]++;
            }
            this.confirmProduct(this.numObj[p.id+'-'+n.id]);

            // this.changeMask(p, n);
            n.click = !n.click;
        },
        // 隐藏 / 显示 遮罩层
        changeMask: function (p, n) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                this.initProduct(p, n);
            }
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            this.confirmProduct(this.num);
            this.numObj[this.product.id+'-'+this.product.nId] = this.num;
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        },
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            if (this.num > 0) {
                this.num--;
            }

            // this.triggerRemoveProduct(this.num, this.product);
        },
        /*// 从缓存池取出缓存对象
        getProductFromPool: function (p, n) {
            var arr = this.getCartProduct(p, n);
            if (arr.length <= 0) {
                return this.deepCopy(p);
            }

            return arr[0];
        },
        // 拷贝订单对象
        deepCopy: function(p) {
            var result = {};

            for (var key in p) {
                result[key] = typeof p[key]==='object'? this.deepCopy(p[key]): p[key];
            }

            return result;
        },*/
        // 常购清单页面
        toOftenBuyPage: function () {
            window.location.href = './oftenbuy/index.html';
        },
        addProductToCart: function (p) {
            var n = p.normList[0];
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.recommend = {};
            this.product = this.getProductFromPool(p, n);
            if(this.cartProducts.indexOf(this.product) == -1) {
                this.product.unitPrice = n.price;
                this.product.unit = n.unit;
                this.product.nId = n.id;
                this.cartProducts.push(this.product);
            } else {
                this.product.num++;
            }
        },
        touchProductToCart: function (p, event) {
            this.recommend = this.deepCopy(p);
            return false;
        }
    },
    mounted: function () {
        this.getRecommends();
        // 获得panel的高度
        this.panelHeight = $('.products-content').height() / 2;

        this.getConsumerHotline();
        this.getOftenBuyList();
        this.getCartProducts();
    }
});